package cn.imuduan.wechatrobot;

import cn.imuduan.dao.InterestingDao;
import cn.imuduan.pojo.MyBean;
import cn.zhouyafeng.itchat4j.Wechat;
import cn.zhouyafeng.itchat4j.api.MessageTools;
import cn.zhouyafeng.itchat4j.api.WechatTools;
import cn.zhouyafeng.itchat4j.beans.BaseMsg;
import cn.zhouyafeng.itchat4j.face.IMsgHandlerFace;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Random;

/**
 * @author MuDuan
 * @date 2020/1/24 21:26
 */
public class Interesting implements IMsgHandlerFace {

    private static org.slf4j.Logger log = LoggerFactory.getLogger(Interesting.class);
    private static InterestingDao myBeanDao;

    // TODO: 2020/1/24 未能实现主动给人或群聊发送消息
    public static void main(String[] args) {
        String qrPath = "D:\\itchat4j\\login";
        IMsgHandlerFace Interesting = new Interesting();
        Wechat wechat = new Wechat(Interesting, qrPath);
        wechat.start();
    }

    static {
        log.info("Mybatis环境搭建中...");
        String resource = "mybatis.xml";
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
            myBeanDao = sqlSession.getMapper(InterestingDao.class);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Mybatis环境搭建失败...");
        }
//        new ClassPathXmlApplicationContext("spring-jobs.xml"); 开启定时任务
    }

    // @@46be5434a8722ae94db2ad696275e2dba2665b78f77128221c8b95547b3aa8f5
    // @@fdd249e389840d53ec2cba46ae991ce4d4bf8e5b99d6f6f43520b93b3d0e8fe4
    public void myMsgHandle() {
        log.info("开始发送大年执行任务...");
        MessageTools.sendMsgByNickName("1","我与下棋势不两立");
        //获取群列表ID
        List<String> groupIdList = WechatTools.getGroupIdList();
        List<JSONObject> groupList = WechatTools.getGroupList();
        System.out.println(groupIdList);
        System.out.println(groupList);
        //获取数据库中数据
        List<MyBean> list = myBeanDao.findByCondition();
        //实现每次执行任务随机输出1条数据
        if (list.size() > 0) {
            int i = new Random().nextInt(list.size());
            String content = list.get(i).getContent(); //内容
            String groupId = groupIdList.get(0);       //群聊ID
//        MessageTools.sendMsgById(content, groupId);
            list.remove(i);
        }
        if (list.size() == 0) {
            //任务完成，退出微信
            System.out.println("任务完成，退出微信");
            WechatTools.logout();
        }
    }

    @Override
    public String textMsgHandle(BaseMsg msg) {
        String content = "";
        if ("1278".equalsIgnoreCase(msg.getContent())) {
            //获取数据库中数据
            List<MyBean> list = myBeanDao.findByCondition();
            //实现每次执行任务随机输出1条数据
            int i = new Random().nextInt(list.size());
            content = list.get(i).getContent(); //内容
        }
        return content;
    }

    @Override
    public String picMsgHandle(BaseMsg msg) {
        return null;
    }

    @Override
    public String voiceMsgHandle(BaseMsg msg) {
        return null;
    }

    @Override
    public String viedoMsgHandle(BaseMsg msg) {
        return null;
    }

    @Override
    public String nameCardMsgHandle(BaseMsg msg) {
        return null;
    }

    @Override
    public void sysMsgHandle(BaseMsg msg) {
    }

    @Override
    public String verifyAddFriendMsgHandle(BaseMsg msg) {
        return null;
    }

    @Override
    public String mediaMsgHandle(BaseMsg msg) {
        return null;
    }

}
